/**
 * LIOM Laboratory, Dec 20, 2012
 * Created by: flesage
 */
package ca.polymtl.liom.fileio;

import java.lang.String;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.*;

/**
 * @author flesage
 * This class is used to define the base directory, and sub-directories of a given
 * experiment. Sub-directories are automatically named with experiment numbers E01, E02,
 * etc..
 *
 */
public class LIOMDirectoryNameSelector {


	private int p_current_experiment;
	private String p_base_name;
	private String p_base_dir;
	private String p_current_dir;

/**
 * This constructor will take the directory specified as 'base' and will create sub-
 * directories for experiments starting with the short 2-letter name
 * of the experiment set. If date is true, the current date will be added to the directory
 * name automatically. 
 * @param base_dir Base directory in which all sub-folders will be created
 * @param exp_name Short name for this experiment
 * @param date If true, the date will be appended to the two letter set.
 */
	public LIOMDirectoryNameSelector( String base_dir, String exp_name, boolean date )
	{
		p_current_experiment = 0;
		p_base_name="E";
		p_base_dir = base_dir+File.separator+exp_name;
		if( date )
		{
			DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			String date_dir=df.format(new Date());
			p_base_dir+="--"+date_dir;
		}
		// Create base experiment directory 
		// XXX: Add logger, add try catch 
		boolean success = new File(p_base_dir).mkdir();
		
		if (success)
		{
			System.out.println("Experiment Directory created.");
		}
	}
	
	/**
	 * This function creates a new save folder for the current experiment which will 
	 * be located under the base directory. Folder names will be numbered: E001, E002, etc
	 * for each new call. The full path of the new folder is returned after creation.
	 * @return The name of the created folder.
	 */
	public String getNextFolder()
	{
		p_current_experiment += 1;
		String tmp = String.format("%03d",p_current_experiment);
		// Add path, create dir
		String new_dir = p_base_dir+File.separator+p_base_name+tmp;
		boolean success = new File(new_dir).mkdir();
		if (success)
		{
			p_current_dir = new_dir;
		}		
		return p_current_dir;
	}
	
	/**
	 * 
	 * @return This function returns the current folder for saving experiment data.
	 */
	public String getCurrentFolder()
	{
		return p_current_dir;
	}	
	/**
	 * 
	 * @return Value of the base directory for this experiment.
	 */
	public String getBaseDir()
	{
		return p_base_dir;
	}	
}
